package com.emps_vue.demo.Controller;

import com.emps_vue.demo.Entity.Emp;
import com.emps_vue.demo.Service.EmpService;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("emp")
@Slf4j
public class EmpController {
    @Resource
    private EmpService empService;
    @Value("${photo.dir}")
    private String realpath;

    @RequestMapping("findAll")
    public List<Emp> findAll() {
        return empService.findAll();
    }

    @RequestMapping("save")
    public Map<String, Object> save(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息: [{}]", emp.toString());
        log.info("头像信息: [{}]", photo.getOriginalFilename());
        Map<String, Object> map = new HashMap<>();
        String NewFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
        emp.setPath(NewFileName);
        try {
            photo.transferTo(new File(realpath, NewFileName));
            map.put("state", true);
            map.put("msg", "插入成功!点击确定跳转至员工页面");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "插入失败!");
        }
        empService.save(emp);
        return map;
    }

    @RequestMapping("delete")
    public Map<String, Object> delete(String id) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            Emp emp = new Emp();
            emp = empService.findOne(id);
            File file = new File(realpath, emp.getPath());
            if (file.exists()) file.delete();
            empService.delete(id);
            map.put("state", true);
            map.put("msg", "删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "删除失败!");
        }
        return map;
    }

    @RequestMapping("findOne")
    public Emp findOne(String id) {

        Emp emp = new Emp();

        emp = empService.findOne(id);
        return emp;

    }

    @RequestMapping("update")
    public Map<String, Object> updete(Emp emp, MultipartFile photo) {

        HashMap<String, Object> map = new HashMap<>();
        try {

            if (photo != null && photo.getSize() != 0) {
                String filename = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
                File file = new File(realpath, emp.getPath());
                if (file.exists()) file.delete();
                emp.setPath(filename);
                filename = emp.getPath();
                photo.transferTo(new File(realpath, filename));
            }

            empService.update(emp);
            map.put("state", true);
            map.put("msg", "更新成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "更新失败!");
        }
        return map;
    }
}
